package com.wxiao.exam.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wxiao.exam.ClassService;
import com.wxiao.exam.PageBean;
import com.wxiao.exam.Result;
import com.wxiao.exam.mapper.ClassMapper;
import com.wxiao.exam.pojo.ClassDO;
import com.wxiao.exam.pojo.vo.ClassListVO;
import com.wxiao.exam.pojo.vo.ClassVO;
import com.wxiao.exam.pojo.vo.ExamVO;
import com.wxiao.exam.pojo.vo.TeacherVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * <br/>
 * Created by wangxiao on 2019/07/07
 */
@Service("classService")
public class ClassServiceImpl implements ClassService {

    private final static Logger LOG = LoggerFactory.getLogger(ClassServiceImpl.class);

    @Resource
    private ClassMapper classMapper;

    @Override
    public Result addClass(ClassVO classVO) throws Exception {
        if (findGradeByCno(classVO)) {
            return Result.getFailure("此班级已经存在");
        }
        //保存到数据库
        int count = classMapper.addClass(classVO);
        if (count > 0) {
            return new Result(Result.CODE_SUCCESS, "添加班级成功");
        }
        throw new RuntimeException("添加错误");
    }

    /**
     * 根据条件查询班级是否存在
     *
     * @param classVO 班级信息
     * @return true 存在， false 不存在
     */
    public boolean findGradeByCno(ClassVO classVO) {
        List<ClassDO> classDOS = classMapper.listClass(classVO);
        if (classDOS != null && !classDOS.isEmpty()) {
            return true;
        }
        return false;
    }

    @Override
    public Result updateClass(ClassVO classVO) throws Exception {
        return null;
    }

    @Override
    public Result findDetailClass(ClassVO classVO) throws Exception {
        return null;
    }

    @Override
    public PageBean<ClassListVO> listClass(ClassVO query) throws Exception {
        //防止页码越界
        Integer count = classMapper.countClass(query);
        int max = (count + query.getPageSize() - 1) / query.getPageSize();
        //为了考虑 数据库中只有1-5条数据时
		/*if (query.getPageCode() > max) {
			query.setPageCode(max);
		}
*/
        PageHelper.startPage(query.getPageCode(), query.getPageSize());
        List<ClassListVO> classLists = classMapper.listClazzAssociation(query);

        //插件提供的分页信息
        PageInfo<ClassListVO> info = new PageInfo<>(classLists);

        //根据插件提供的分页信息，构造我们自己的分页信息
        PageBean<ClassListVO> pageBean = new PageBean<ClassListVO>(classLists, info.getPageSize()
                , query.getPageCode(), (int) info.getTotal(), query.getSize());
        System.out.println(classLists);
        return pageBean;
    }

    @Override
    public Result listClassPage(ClassVO classVO) throws Exception {
        return null;
    }

    @Override
    public Result countClass(ClassVO classVO) throws Exception {
        return null;
    }

    @Override
    public Result deleteClass(ClassVO classVO) throws Exception {
        return null;
    }

    @Override
    public List<ClassDO> listByTeacher(TeacherVO teacherVO) {
        return classMapper.listByTeacher(teacherVO);
    }

    @Override
    public List<ClassDO> listClass2(ClassVO classVO) {
        return classMapper.listClass(classVO);
    }

    @Override
    public Result listClassByExam(ExamVO examVO) {
        List<ClassListVO> classListVOS = classMapper.listClassByExam(examVO);
        return new Result(Result.CODE_SUCCESS, classListVOS);
    }

}